package ec.linde.modulos.recursos.uploadImg;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;



public class FileUpload {
	
	protected ArrayList<File> files = new ArrayList<File>();
	private int uploadsAvailable = 1;
	private boolean autoUpload = false;
		
	public FileUpload() {
		
	}
	
	public int getSize() {
		if (getFiles().size()>0){
			return getFiles().size();
		}else 
		{
			return 0;
		}
	}

	public synchronized void paint(OutputStream stream, Object object) throws IOException {
		stream.write(getFiles().get((Integer)object).getData());
	}
	public synchronized void listener(FileUploadEvent event) throws Exception{
	    UploadedFile item = event.getFile();
	    File file = new File();
	    file.setLength(item.getContents().length);
	    file.setName(item.getFileName());
	    file.setData(item.getContents());
	    files.add(file);
	    uploadsAvailable--;
	}
	
	public String clearUploadData() {
		files.clear();
		setUploadsAvailable(5);
		return null;
	}
	
	public long getTimeStamp(){
		return System.currentTimeMillis();
	}
	
	public ArrayList<File> getFiles() {
		return files;
	}

	public void setFiles(ArrayList<File> files) { 
		this.files = files;
	}

	public int getUploadsAvailable() {
		return uploadsAvailable;
	}

	public void setUploadsAvailable(int uploadsAvailable) {
		this.uploadsAvailable = uploadsAvailable;
	}

	public boolean isAutoUpload() {
		return autoUpload;
	}

	public void setAutoUpload(boolean autoUpload) {
		this.autoUpload = autoUpload;
	}

	
	
	
}
